package paneles;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFormattedTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import datos.Servicio;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PanelServ extends javax.swing.JPanel {
	
	private JButton bCancelar;
	private JButton bAceptar;
	private JTextField tDescripcion;
	private JTextField tNombre;
	private JFormattedTextField fPrecioProducto;
	private JLabel lPrecioXUnidad;
	private JComboBox jComboBox1;
	private JLabel lNombre;
	private JLabel lPrecio;
	private JTextField tPrecio;
	private JLabel lTipo;
	private JLabel lDescripcion;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new PanelServ());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public PanelServ() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			FlowLayout thisLayout = new FlowLayout();
			thisLayout.setVgap(10);
			thisLayout.setHgap(10);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(315, 200));
			{
				lNombre = new JLabel();
				this.add(lNombre);
				lNombre.setText("Nombre del Servicio");
				lNombre.setHorizontalAlignment(SwingConstants.CENTER);
				lNombre.setPreferredSize(new java.awt.Dimension(130, 21));
				lNombre.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lNombre.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tNombre = new JTextField();
				this.add(tNombre);
				tNombre.setPreferredSize(new java.awt.Dimension(160, 21));
				tNombre.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			}
			{
				lPrecio = new JLabel();
				this.add(lPrecio);
				lPrecio.setText("Precio");
				lPrecio.setHorizontalAlignment(SwingConstants.CENTER);
				lPrecio.setPreferredSize(new java.awt.Dimension(130, 21));
				lPrecio.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lPrecio.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tPrecio = new JTextField();
				this.add(tPrecio);
				tPrecio.setPreferredSize(new java.awt.Dimension(160, 21));
				tPrecio.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			}
			{
				lDescripcion = new JLabel();
				this.add(lDescripcion);
				lDescripcion.setText("Descripcion");
				lDescripcion.setHorizontalAlignment(SwingConstants.CENTER);
				lDescripcion.setPreferredSize(new java.awt.Dimension(130, 21));
				lDescripcion.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lDescripcion.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tDescripcion = new JTextField();
				this.add(tDescripcion);
				tDescripcion.setPreferredSize(new java.awt.Dimension(160, 21));
				tDescripcion.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			}
			{
				lTipo = new JLabel();
				this.add(lTipo);
				lTipo.setText("Tipo de Servicio");
				lTipo.setHorizontalAlignment(SwingConstants.CENTER);
				lTipo.setPreferredSize(new java.awt.Dimension(130, 21));
				lTipo.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lTipo.setForeground(new java.awt.Color(0,0,0));
			}
			{
				bCancelar = new JButton();
				this.add(getJComboBox1());
				this.add(getLPrecioXUnidad());
				this.add(getFPrecioProducto());
				{
					bAceptar = new JButton();
					this.add(bAceptar);
					this.add(bCancelar);
					bAceptar.setText("Aceptar");
					bAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
					bAceptar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
						}
					});
				}
				
				bCancelar.setText("Cancelar");
				bCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public JButton getBCancelar() {
		return bCancelar;
	}

	public void setBCancelar(JButton cancelar) {
		bCancelar = cancelar;
	}

	public JButton getBAceptar() {
		return bAceptar;
	}

	public void setBAceptar(JButton aceptar) {
		bAceptar = aceptar;
	}

	public Servicio getServicio() {
		Servicio s=new Servicio();
		s.setNombre(this.tNombre.getText());
		if (!this.tPrecio.getText().equals(""))
			s.setPrecio(Double.valueOf(this.tPrecio.getText()));
		else
			s.setPrecio(Double.valueOf(0));
		if(this.jComboBox1.getSelectedItem().toString().equals("Color"))
			s.setPrecioProducto(Double.valueOf(this.fPrecioProducto.getValue().toString()));
		s.setDescripcion(this.tDescripcion.getText());
		s.setTipo(this.jComboBox1.getSelectedItem().toString());
		return s;
	}
	
	private JComboBox getJComboBox1() {
		if(jComboBox1 == null) {
			ComboBoxModel jComboBox1Model = 
				new DefaultComboBoxModel(
						new String[] { "Corte", "Color" });
			jComboBox1 = new JComboBox();
			jComboBox1.setModel(jComboBox1Model);
			jComboBox1.setPreferredSize(new java.awt.Dimension(160, 21));
			jComboBox1.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			jComboBox1.setBackground(new java.awt.Color(255,255,255));
			jComboBox1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					if(((jComboBox1.getSelectedItem()).toString()) == "Color"){
						fPrecioProducto.setEnabled(true);
					}
					else
						fPrecioProducto.setEnabled(false);
				}
			});
		}
		return jComboBox1;
	}

	public void setServicio(Servicio s) {
		this.tNombre.setText(s.getNombre());
		if(s.getTipo().equals("Color"))
			this.fPrecioProducto.setValue(s.getPrecioProducto());
		this.tDescripcion.setText(s.getDescripcion());
		this.tPrecio.setText(String.valueOf(s.getPrecio()));
		this.jComboBox1.setSelectedItem(s.getTipo());
	}

	private JLabel getLPrecioXUnidad() {
		if(lPrecioXUnidad == null) {
			lPrecioXUnidad = new JLabel();
			lPrecioXUnidad.setText("Precio por Unidad de Producto");
			lPrecioXUnidad.setPreferredSize(new java.awt.Dimension(210, 21));
			lPrecioXUnidad.setHorizontalAlignment(SwingConstants.CENTER);
			lPrecioXUnidad.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			lPrecioXUnidad.setForeground(new java.awt.Color(0,0,0));

		}
		return lPrecioXUnidad;
	}
	
	private JTextField getFPrecioProducto() {
		if(fPrecioProducto == null) {
			fPrecioProducto = new JFormattedTextField();
			fPrecioProducto.setPreferredSize(new java.awt.Dimension(80, 21));
			NumberFormat vFormat = NumberFormat.getCurrencyInstance();
			fPrecioProducto.setValue(new Double(0));
			NumberFormat eFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
			eFormat.setGroupingUsed(false);
			NumberFormatter dvFormat = new NumberFormatter(vFormat);
			NumberFormatter deFormat = new NumberFormatter(eFormat);
			DefaultFormatterFactory fact = new DefaultFormatterFactory(dvFormat,dvFormat,deFormat);
			fPrecioProducto.setFormatterFactory(fact);
			fPrecioProducto.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
			fPrecioProducto.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			fPrecioProducto.setEnabled(false);
			fPrecioProducto.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					if(evt.getKeyCode() == KeyEvent.VK_ENTER){
						fPrecioProducto.setFocusable(false);
						fPrecioProducto.setFocusable(true);
					}
				}
			});
		}
		return fPrecioProducto;
	}

}
